# -*- mode: python -*-
from sys import platform
import os

src_dll = ["basics.cpp","command_line.cpp","enabler.cpp","files.cpp",
           "find_files_posix.cpp","graphics.cpp","init.cpp","interface.cpp",
           "keybindings.cpp","music_and_sound_openal.cpp", "random.cpp",
           "textlines.cpp","glew.c","enabler_input.cpp","ViewBase.cpp",
           "KeybindingScreen.cpp", "win32_compat.cpp", "textures.cpp",
           "resize++.cpp", "renderer_offscreen.cpp",
           "ttf_manager.cpp"]

dll  = '#libs/libgraphics.so'

optimize = False
debug = True
profile = False

clang = False

libgraphics = True
tetris = False

M64 = False if libgraphics else True

# Propagate PATH. We'll just take our chances with non-repeatable builds.
env = Environment(ENV = {'PATH' : os.environ['PATH']})

if clang:
   env["STATIC_AND_SHARED_OBJECTS_ARE_THE_SAME"]=True
   env['CC']="clang"
   env['CXX']="clang++"
   env['CXXCOM']="$CXX -o $TARGET -emit-llvm -pipe -c $CXXFLAGS $CCFLAGS $_CCCOMCOM $SOURCES"
   env['CCCOM']="$CC -o $TARGET -emit-llvm -pipe -c $CFLAGS $CCFLAGS $_CCCOMCOM $SOURCES"
   env['SHCCCOM']=env['CCCOM']
   env['SHCXXCOM']=env['CXXCOM']
   env["OBJSUFFIX"]=".bc"
   env["SHOBJSUFFIX"]=".so.bc"
   env["ARCOM"]="llvm-link $SOURCES -o $TARGET"
   env["RANLIBCOM"]="true"
   if optimize:
      env["SHLINKCOM"]="llvm-link $SOURCES | opt -disable-internalize -std-link-opts | llc -O3 | g++ -shared -xassembler - -o $TARGET $SHLINKFLAGS $_LIBDIRFLAGS $_LIBFLAGS"
      env["LINKCOM"]="llvm-link $SOURCES | opt -disable-internalize -std-link-opts | llc -O3 | g++ -xassembler -o $TARGET - $LINKFLAGS $_LIBDIRFLAGS $_LIBFLAGS"
   else:
      env["SHLINKCOM"]="llvm-link $SOURCES | llc | g++ -shared -xassembler -fPIC -o $TARGET - $SHLINKFLAGS $_LIBDIRFLAGS $_LIBFLAGS"
      env["LINKCOM"]="llvm-link $SOURCES | llc | g++ -xassembler -o $TARGET - $LINKFLAGS $_LIBDIRFLAGS $_LIBFLAGS"


# Init variables, and make sure they're around for appending to
# These variables are the ones used by the actual compilation. We construct
# them from ccflags/libs in varying ways depending on which compiler is used,
# below.
env['CPPPATH']=["#g_src/fmodexinclude"]
env['CCFLAGS']=["-Dunix"]
env['CXXFLAGS'].append("-std=gnu++0x")
env['LIBS']=["SDL_image","z","SDL_ttf"]
# If there's a library in the libs folder it will get linked over a system one.
env['LIBPATH']=["#libs"]
if not M64:
   env['LIBPATH'].append("/usr/lib32")
env['LINKFLAGS']=["-Wl,--as-needed",'-Wl,-rpath=\$$ORIGIN/libs']

# GLEW
env["CPPPATH"].append('#g_src/glext')
env["CCFLAGS"].append('-DGLEW_STATIC') # Should only be needed on windows, but can't hurt.

# Generic unix, including Linux
if not clang and not M64:
   env['ASFLAGS']="--32"
   env['LINKFLAGS'].append("-m32")
   env["CCFLAGS"].append("-m32")
env.ParseConfig('pkg-config gtk+-2.0 --cflags --libs')
env.ParseConfig('pkg-config openal --cflags')
env.ParseConfig('pkg-config sndfile --cflags')
env.ParseConfig('pkg-config sdl --cflags --libs')
env.ParseConfig('pkg-config glu --cflags --libs')


if profile:
   env["CCFLAGS"].append("-pg")
   env['LINKFLAGS'].append("-pg")

if optimize:
   if not clang:
      if not debug:
         env["CCFLAGS"]+=["-fomit-frame-pointer"]
      if not M64:
         env["CCFLAGS"]+=["-O3","-march=pentium3","-mfpmath=sse"]
      else:
         env["CCFLAGS"]+=["-O3"]
   else:
      env["CCFLAGS"].append("-O2") # Only to reduce WPO time

if debug:
   env["CCFLAGS"].append("-ggdb")
   env["CCFLAGS"].append("-DDEBUG")
   
# Finally, do the build
if libgraphics:
   env.SharedLibrary(dll, src_dll)

if tetris:
   env.Program("#tetris", src_dll + ["tetris.cpp"])
